using System;

namespace Cfg.ValidationAttributes
{
    public class MaxLengthAttribute : CfgFinalizedStringValueValidatorBaseAttribute
    {
        public int MaxLength { get; private set; }
        public MaxLengthAttribute(int maxLength, params string[] contexts)
        {
            Contexts = contexts;
            MaxLength = maxLength;
        }

        protected override void Validate(string finalizedStringValue)
        {
            if (finalizedStringValue == null)
                finalizedStringValue = string.Empty;

            if (finalizedStringValue.Length > MaxLength)
            {
                var message = string.Format("Maximum length is set to({0}), but value '{1}' has length {2}",
                    MaxLength,
                    finalizedStringValue,
                    finalizedStringValue.Length);

                throw new Exception(message);
            }
        }
    }
}